package ru.spies.work;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import ru.spies.dbwork.DBWork;
import ru.spies.entities.ITableClass;
import ru.spies.requests.UpdateRequestWork;


public class UpdateWork extends AbstractWork{
	
	private static UpdateWork statThis=null;

	private UpdateWork() {
		
	}
	
	public static UpdateWork getInstance(){
		if (statThis==null){
			statThis = new UpdateWork();
		}
		return statThis;
	}

	@Override
	protected void doWorkWithAgent(HttpServletRequest request) {
		UpdateObjects(UpdateRequestWork.getInstance().getAgents(request));
	}

	@Override
	protected void doWorkWithAgentAndTask(HttpServletRequest request) {
		UpdateObjects(UpdateRequestWork.getInstance().getAgentsAndTasks(request));
	}

	@Override
	protected void doWorkWithCountry(HttpServletRequest request) {
		UpdateObjects(UpdateRequestWork.getInstance().getCountries(request));
	}

	@Override
	protected void doWorkWithDepartment(HttpServletRequest request) {
		UpdateObjects(UpdateRequestWork.getInstance().getDepartments(request));
	}

	@Override
	protected void doWorkWithDocument(HttpServletRequest request) {
		UpdateObjects(UpdateRequestWork.getInstance().getDocuments(request));	
	}

	@Override
	protected void doWorkWithImportance(HttpServletRequest request) {
		UpdateObjects(UpdateRequestWork.getInstance().getImportances(request));
	}

	@Override
	protected void doWorkWithTask(HttpServletRequest request) {
		UpdateObjects(UpdateRequestWork.getInstance().getTasks(request));
	}

	@Override
	protected void doWorkWithTaskType(HttpServletRequest request) {
		UpdateObjects(UpdateRequestWork.getInstance().getTaskTypes(request));
	}
	
	private void UpdateObjects(Collection<ITableClass> objects){
		if (objects==null) return;
		if (objects.size()!=0){
			for (ITableClass object : objects) {
				DBWork.getInstance().updateTableObject(object);
			}
		}
	}

}
